package crypt

import (
	"log"
)

// StrDecrypt 字符串解密--以ENC开头的字符串为加密字符串，解密后返回解密后的字符串
func StrDecrypt(str, strKey string) (strDecrypt string) {
	log.Printf("原始字符串：--%s \n", str)
	log.Printf("盐值字符串：--%s \n", strKey)

	if str[:3] == "ENC" {
		sourceStr := substr(str, 4, len(str)-1)
		log.Printf("加密--源字符串：--%s \n", sourceStr)
		strDecrypt = Decrypt(sourceStr, strKey)
	} else {
		strDecrypt = str
	}

	log.Printf("解密--字符串：--%d \n", len(strDecrypt))
	return strDecrypt
}

// substr 字符串截取
func substr(str string, start int, end int) string {
	rs := []rune(str)
	length := len(rs)
	if start < 0 || start > length {
		return ""
	}

	if end < 0 || end > length {
		return ""
	}

	if start >= end {
		return ""
	}

	return string(rs[start:end])
}
